package com.hmdp.config;

import com.hmdp.utils.LoginInterceptor;
import com.hmdp.utils.RefreshTokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;


@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    //添加拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        //第一层拦截器，RefreshTokenInterceptor,目的为了刷新Token的有效时间
        registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate)).addPathPatterns("/**").order(0);

        //registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**");
        //第二层拦截器，LoginInterceptor ，目的为了拦截 未登录时候状态
        registry.addInterceptor(new LoginInterceptor()).
        excludePathPatterns(
                "/user/code",
                "/user/login",
                "/shop/**",
                "/voucher/**",
                "/upload/**",
                "/shop-type/**",
                "/blog/hot"
        ).order(1); //除了上面可以默认放行，其他的都需要登录校验成功才放行

        //order（num），表示拦截器的优先级，数字越小优先级越高
    }
}
